var startWebLinkDom = document.getElementById('startWebLink');
var stopWebLinkDom = document.getElementById('stopWebLink');
var ws;

startWebLinkDom.addEventListener('click', function(e) {
	ws = new WebSocket('ws://1.1.1.1:101/');
                                             
	ws.onopen = function(event){
		ws.send('{"device_id":"1234567"}');
    }
                                             
    ws.onmessage = function(event) {
    		//alert(typeof event.data);
    	//	alert(event.data);
    		if (event.data != 'pong'){
    			var jsonData = JSON.parse(event.data);
    		//	alert('Count is: ' + jsonData);
    		}
        $('.jke-ecgChart').ecgChart('addDataPoint', { x: Date.now(), y: JSON.parse(event.data).value });
    };
                                             
   ws.onclose = function(evt) { //绑定关闭或断开连接事件
        console.log('Connection closed.');
   };
   
   //定时发送ping
   setTimeout(function(e){
   		ws.send('ping');
   },1000);
                 
});


stopWebLinkDom.addEventListener('click', function(e) {
   ws.close();
});
